package com.example.business.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.business.entity.CartItem;
import com.example.business.entity.Product;
import com.example.business.entity.ProductSku;
import com.example.business.request.UpdateCartItemAmountRequest;
import com.example.business.service.ProductService;
import com.example.business.service.ProductSkuService;
import com.example.common.response.PageResponse;
import com.example.common.response.R;
import com.example.business.request.CartItemListRequest;
import com.example.business.response.CartItemResponse;
import com.example.business.service.CartItemService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.business.request.CartItemRequest;
import java.util.List;
import com.example.business.response.ProductResponse;
import com.example.business.response.ProductSkuResponse;

@RestController
@RequestMapping("cart-item")
public class CartItemController {
    @Autowired
    CartItemService cartItemService;

    @Autowired
    ProductSkuService productSkuService;

    @Autowired
    ProductService productService;

    @GetMapping("list")
    public R<PageResponse<CartItemResponse>> list(@Valid CartItemListRequest request) {
        PageResponse<CartItemResponse> list = cartItemService.list(request);
        // 遍历每个购物车项，补充商品和 SKU 详情
        for (CartItemResponse item : list.getList()) {
            // 查询 SKU 详情
            ProductSku sku = productSkuService.findById(item.getProductSkuId());
            if (sku != null) {
                ProductSkuResponse skuResp = BeanUtil.copyProperties(sku, ProductSkuResponse.class);
                item.setProductSku(skuResp);

                // 查询商品详情
                Product product = productService.findById(sku.getProductId());
                if (product != null) {
                    ProductResponse prodResp = BeanUtil.copyProperties(product, ProductResponse.class);
                    item.setProduct(prodResp);
                }
            }
        }
        return R.ok(list);
    }

    @PostMapping("save")
    public R<Object> save(@Valid @RequestBody CartItemRequest request) {
        ProductSku productSku = productSkuService.findById(request.getProductSkuId());
        if (productSku == null) {
            return R.failValid("该商品不存在");
        } else {
            if (productSku.getStock() == 0) {
                return R.failValid("该商品已售完");
            }
            Product product = productService.findById(productSku.getProductId());
            if (!product.getOnSale()) {
                return R.failValid("该商品未上架");
            }
        }

        // 商品是否已经存在该用户的购物车中
        CartItem cartItem = cartItemService.getCartItemByProductSkuId(request.getProductSkuId());
        if (cartItem != null) {
            if (request.getAmount() > 0 && productSku.getStock() <= cartItem.getAmount()) {
                return R.failValid("该商品库存不足");
            }

            // id 有值，会执行更新操作
            request.setId(cartItem.getId());
            // 叠加商品数量
            request.setAmount(request.getAmount() + cartItem.getAmount());
        }

        cartItemService.save(request);
        return R.ok();
    }

    @DeleteMapping("delete")
    public R<Integer> delete(@RequestBody List<Long> ids) {
        int result = cartItemService.deleteBatch(ids);
        return R.ok(result);
    }

    @PostMapping("update-cart-item-amount")
    public R<Object> updateCartItemAmount(@RequestBody @Valid UpdateCartItemAmountRequest request) {
        CartItem cartItem = cartItemService.getCartItemByProductSkuId(request.getProductSkuId());
        if (cartItem == null) {
            return R.fail("商品 SKU 不存在");
        }

        cartItem.setAmount(cartItem.getAmount() + request.getFactor());

        ProductSku productSku = productSkuService.findById(request.getProductSkuId());
        if (productSku.getStock() < cartItem.getAmount()) {
            return R.failValid("该商品库存不足");
        }

        int result = cartItemService.updateCartItemAmount(cartItem);

        return R.ok(result);
    }
}
